import engine
import pygame
import json
from pygame.key import *
from pygame.locals import *
from pygame.color import *


import pymunk
import pymunk.constraints
import pymunk.pygame_util


class game_pack:
    def __init__(self):
        pygame.init()
        self.env = engine.guide_wire_space()
        self.screen = pygame.Surface((700, 600))
        self.display = pygame.display.set_mode((700, 600))
        self.draw_options = pymunk.pygame_util.DrawOptions(self.screen)
        with open("real.json") as json_file:
            self.wall_poslist = json.load(json_file)	
        self.insert_limit = 50
        self.bg = pygame.image.load("img_rsc\\3.png")
        # 载入血管壁
        self.env.draw_walls(self.wall_poslist)
        # 载入导丝
        self.env.create_goal((310, 485))
        self.env.create_ini_guidewire(radius=6, ini_len=5)


    def events_handler(self):
        for event in pygame.event.get():
            if event.type == QUIT:
                exit()
            if event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    exit()
                if event.key == K_d:
                    if len(self.env.balls) <= self.insert_limit:
                        self.env.insert_a_ball(radius=6,)
                if event.key == K_a:
                    self.env.pull_back_a_ball()
                if event.key == K_w:
                    self.env.bend_guidewire(d_angle=10)
                if event.key == K_s:
                    self.env.bend_guidewire(d_angle=-10)
            if event.type == MOUSEMOTION:
                pygame.display.set_caption(f"Mouse At: {event.pos}")


    def render(self):
        # pgimg = pygame.image.load("img_rsc\\3_real.png")
        self.display.blit(self.bg, (0,0))
        points = []
        # 获取导丝的位姿信息并绘制导丝
        for i in self.env.balls:
            points.append(i.body.position.int_tuple)
        pygame.draw.lines(self.display, (100, 100, 100), False, points, width=5)
        pygame.draw.circle(self.display, (155, 155, 155), points[-1], 3)
        # 绘制肿瘤
        for i in self.env.goals:		
            p = self.env.goals[-1].body.position.int_tuple
            pygame.draw.circle(self.display, (70, 27, 44), p, 15)
        # 绘制人工智能识别的结果
        w, h = 40, 40
        rect = (points[-1][0] - w/2, points[-1][1] - h/2, w, h)
        pygame.draw.rect(self.display, (0, 255, 0), rect, 5)
        rect = (p[0] - w/2, p[1] - h/2, w, h)
        pygame.draw.rect(self.display, (255, 255, 0), rect, 5) 
        # 刷新屏幕
        pygame.display.flip()

	
    def debug_render(self):
        self.screen.blit(self.bg, (0,0))          # 清空图案
        self.env.space.debug_draw(self.draw_options)  # 绘制新图案
        self.display.blit(self.screen, (0,0))
        pygame.display.flip()


def main():
	game = game_pack()
	FPS = 60
	exact = 1000 / 60
	clock = pygame.time.Clock()
	while True:
		# 计算下一帧
		for x in range(int(exact)):
			game.env.space.step(1 / FPS / exact)
		game.render()
		# game.debug_render()
		game.events_handler()
		clock.tick(FPS)
		



if __name__ == '__main__':
	main()